import { message } from "@/utils/message";
import { updateProductStatus } from "@/api/productInfo";

import { computed } from "vue";

// 需是hooks写法（函数中有return），避免失去响应性
export function useColumnsNow(
  productList: any,
  handleViewCallback?: (row: any) => void,
  handleEditCallback?: (row: any) => void,
  handleRefreshCallback?: () => void
) {
  const tableDataNow = computed(() => productList.value || []);
  const columnsNow: TableColumnList = [
    {
      label: "产品名称",
      prop: "product_name_cn"
    },
    {
      label: "产品配图",
      prop: "product_img",
      cellRenderer: ({ row }) => {
        const BASE_URL = "https://exhibition-bucket.oss-cn-beijing.aliyuncs.com";
        const imageUrl = row.product_img
          ? `${BASE_URL}${row.product_img}`
          : "/placeholder-image.png";
        return (
          <img
            src={imageUrl}
            alt="产品配图"
            style="width: 60px; height: 60px; object-fit: cover; border-radius: 4px;"
          />
        );
      }
    },
    {
      label: "审批状态",
      prop: "status",
      width: 120,
      align: "center",
      cellRenderer: ({ row }) => {
        let statusText = "";
        let statusType = "";

        switch (row.status) {
          case 0:
            statusText = "审核中";
            statusType = "warning";
            break;
          case 1:
            statusText = "已通过";
            statusType = "success";
            break;
          case 2:
            statusText = "已退回";
            statusType = "danger";
            break;
          case 3:
            statusText = "待提交";
            statusType = "warning";
            break;
          default:
            statusText = "未知";
            statusType = "info";
        }

        return <el-tag type={statusType}>{statusText}</el-tag>;
      }
    },
    {
      label: "操作",
      fixed: "right",
      width: 220,
      cellRenderer: ({ row }) => (
        <>
          <el-button size="small" onClick={() => handleView(row)}>
            查看
          </el-button>
          <el-button size="small" onClick={() => handleEdit(row)}>
            修改
          </el-button>
          <el-button
            round
            size="small"
            type="danger"
            onClick={() => handleSubmit(row)}
          >
            提交审批
          </el-button>
        </>
      )
    }
  ];

  const handleView = (row: any) => {
    if (handleViewCallback) {
      handleViewCallback(row);
    } else {
      message(`您查看了数据：${JSON.stringify(row)}`, {
        type: "success"
      });
    }
  };

  const handleEdit = (row: any) => {
    // 统一拦截提示：0 审核中、1 已通过
    if (row.status === 0) {
      message("产品正在审核中，不能修改", { type: "warning" });
      return;
    }
    if (row.status === 1) {
      message("产品已通过，不能修改", { type: "warning" });
      return;
    }
    if (handleEditCallback) {
      handleEditCallback(row);
    } else {
      message(`您修改了数据：${JSON.stringify(row)}`, {
        type: "success"
      });
    }
  };

  const handleSubmit = async (row: any) => {
    // 状态：0=审核中，1=已通过，2=未通过，3=待提交
    if (row.status === 0) {
      message("已提交，不可重复审批", { type: "warning" });
      return;
    }
    if (row.status === 1) {
      message("已通过，不可重复审批", { type: "warning" });
      return;
    }
    try {
      const result: any = await updateProductStatus(
        row.id,
        row.exhibitor_id,
        0
      );
      if (result.code === 1) {
        message("提交审批成功", { type: "success" });
        // 刷新列表
        if (handleRefreshCallback) {
          handleRefreshCallback();
        }
      } else {
        message(result.msg || "提交审批失败", { type: "error" });
      }
    } catch (error) {
      console.error("提交审批失败:", error);
      message("提交审批失败", { type: "error" });
    }
  };

  return {
    columnsNow,
    tableDataNow
  };
}
